// pages/register/register.js
let app = getApp()
import utils from '../../utils/util'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        loadingHidden: true,
        tips: '',
        registerType: true,
        formData: {
            username: '',
            password: '',
            queryPassword: ''
        }
    },
    changeType() {
        let that = this
        utils.mask(that)
        this.setData({
            registerType: !this.data.registerType
        })
        utils.removeMask(that, 200)
    },

    toLogin() {
        utils.redirectToPage('/pages/login/login', this)
    },
    setTips(msg) {
        this.setData({
            tips: msg
        })
    },
    submitReg() {
        let that = this
        if (that.data.formData.username.trim() && that.data.formData.password.trim()
            && that.data.formData.queryPassword.trim()) {
            if (that.data.formData.queryPassword.trim() === that.data.formData.password.trim()) {
                console.log('校验通过')
                wx.request({
                    url: app.globalData.serviceUrl + '/wx/register',
                    data: {
                        username: that.data.formData.username,
                        password: that.data.formData.password,
                        isStuLogin: that.data.registerType
                    },
                    success: function (res) {
                        let tempData = res.data
                        if (tempData.success) {
                            // app.globalData.userInfo = tempData.data
                            // app.globalData.userType = that.data.registerType ? 1 : 2
                            utils.toast('注册成功')
                            setTimeout(function () {
                                wx.navigateBack({//返回
                                    delta: 1
                                })
                            }, 1000)
                        } else {
                            utils.toast(tempData.msg)
                        }
                    },
                    fail: function () {
                        utils.toast('请求失败')
                    }
                })


            } else {
                that.setTips('密码不一致')
            }
        } else {
            that.setTips('请完善表单')
        }
    },

    /*********************以下为文本框监听***********************/
    watchUsername(e) {
        this.setData({
            ["formData.username"]: e.detail.value
        })
    },
    watchPassword(e) {
        this.setData({
            ["formData.password"]: e.detail.value
        })
    },
    watchQueryPsd(e) {
        this.setData({
            ["formData.queryPassword"]: e.detail.value
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
